var app = getApp()
Page({
  data: {
    pinyin:new Object(),
    result: '',
    val: "",
    show: false,
    key: ''
  },
  onShow: function () {
    this.getKey()
  },
    

getKey() {
  let that = this
  wx.cloud.callFunction({
    name: 'getKey',
    data: {
      name: 'dictionary'
    }
  }).then(res => {
    console.log(res.result.data[0].key)
    that.setData({
      key: res.result.data[0].key
      
    })
  })
},

  

  /**
   * 获取用户输入 
   */
  val: function (e) {
    this.setData({
      val: e.detail.value
    })
  },
  // 事件处理函数
  onInput: function (e) {
    var that = this;
    var char = that.data.val;
    char = char && char.trim();
    if (char.length == 1) {
      wx.request({
        url: "https://v.juhe.cn/xhzd/query?key="+that.data.key+"&word="+that.data.val+"&dtype=json",
        data: {
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          console.log(res)
          wx.hideLoading();
          if (res.statusCode == 200) {
            that.setData({
              result: res.data.result,
              show: true
            })
          } else {
            that.setData({
              result: '服务器错误！',
              show: false
            })
          }
        },
        fail(res){
          console.log(res)
        }
      })
    }
    else {
      this.setData({
        result: '请输入单个汉字！',
        show: true
      });
    }
  },

})
